﻿@model ClinicaMVC3.Models.Funcionario
@{
}
<input  id="inputMethod" type=hidden value="@ViewBag.Method"/>
<h2>@ViewBag.Title - Inserir Novo</h2>
<p>
</p>
<script type="text/javascript">
    $(document).ready
	(
        function () {
            DisableFields(($("#funcao").val() != 3));
            $("#funcao").blur(function () {
                DisableFields(($("#funcao").val() != 3));
            });
        }
    );


    //Esta função será usada para montar um JSON e mandar para o "FuncionarioController"
    function Save() {
        var isValid = false;

        isValid = ($('#formFuncionario').validate().element($('#Nome')));
        isValid = ($('#formFuncionario').validate().element($('#RG'))) ? isValid : false;
        isValid = ($('#formFuncionario').validate().element($('#endereco'))) ? isValid : false;
        isValid = ($('#formFuncionario').validate().element($('#funcao'))) ? isValid : false;
        if ($('#inputMethod').val() != "Edit") 
        {
            isValid = ($('#formFuncionario').validate().element($('#UserName'))) ? isValid : false;
            isValid = ($('#formFuncionario').validate().element($('#Password'))) ? isValid : false;
            isValid = ($('#formFuncionario').validate().element($('#Email'))) ? isValid : false;
            isValid = ($('#formFuncionario').validate().element($('#ConfirmPassword'))) ? isValid : false;
        }

        if (isValid) {

            // Criando um Funcionario Json Object
            var Funcionario = {
                "FuncionarioId": "",
                "Nome": "",
                "RG": "",
                "UserId": "",
                "endereco": "",
                "funcao": "",
                "Consulta": [],
                "FuncionarioEspecialidade": [],
                "FuncionarioTelefone": [],
                "UserName": "",
                "Password": "",
                "Email": ""
            };

            var auxFuncionarioId = 0;
            if ($('#inputMethod').val() == "Edit") {
                auxFuncionarioId = $("#FuncionarioId").val();
            } else {
                auxFuncionarioId = 0;
            }
            Funcionario.FuncionarioId = auxFuncionarioId;
            Funcionario.Nome = $("#Nome").val();
            Funcionario.RG = $("#RG").val();
            Funcionario.endereco = $("#endereco").val();
            Funcionario.funcao = $("#funcao").val();
            Funcionario.UserId = $("#UserId").val();
            Funcionario.UserName = $("#UserName").val();
            Funcionario.Password = $("#Password").val();
            Funcionario.Email = $("#Email").val();

            Funcionario = JSONFuncionarioTelefone(Funcionario, true);

            Funcionario = JSONFuncionarioEspecialidade(Funcionario);

            try {
                $.ajax({
                    url: '/Funcionario/Create',
                    data: JSON.stringify(Funcionario),
                    type: 'POST',
                    contentType: 'application/json;',
                    dataType: 'json',
                    success: function (result) {

                        if (result.Success == "1") {
                            window.location.href = "/Funcionario/index";
                        }
                        else {
                            alert(result.ex);
                        }
                    }
                });
            } catch (e) {
                alert(e.Message);
            }

        }

    }

</script>
@using (Html.BeginForm("Create", "Funcionario", FormMethod.Post, new { id = "formFuncionario" }))
{
    Html.EnableClientValidation(true);
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Informações pessoais</legend>
        @Html.HiddenFor(model => model.FuncionarioId)
        @Html.HiddenFor(model => model.UserId)
        <div class="editor-label">
            @Html.LabelFor(model => model.Nome)
        </div>
        <div class="editor-field">
            @if (ViewBag.Method == "Detail")
            {
                @Html.DisplayFor(model => model.Nome)
            }
            else
            {
                @Html.EditorFor(model => model.Nome)
                @Html.ValidationMessageFor(model => model.Nome)
            }
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.RG)
        </div>
        <div class="editor-field">
            @if (ViewBag.Method == "Detail")
            {
                @Html.DisplayFor(model => model.RG)            
            }
            else
            {
                @Html.EditorFor(model => model.RG)
                @Html.ValidationMessageFor(model => model.RG)
            }
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.endereco)
        </div>
        <div class="editor-field">
            @if (ViewBag.Method == "Detail")
            {
                @Html.DisplayFor(model => model.endereco)
            }
            else
            {
                @Html.EditorFor(model => model.endereco)
                @Html.ValidationMessageFor(model => model.endereco)
            }
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.funcao)
        </div>
        <div class="editor-field">
            @if (ViewBag.Method == "Detail")
            {
                @System.Web.Mvc.Html.MvcHtmlExtensions.FuncaoDisplayFor(Model.funcao.ToString())
            }
            else
            { 
                @Html.FuncaoDropdown(model => model.funcao)
                @Html.ValidationMessageFor(model => model.funcao)                
            }
        </div>
    </fieldset>
    
            ViewBag.ModelClass = "Funcionario";
            ViewBag.Model = Model;
            Html.RenderPartial("TelefoneTable", @ViewData);

            Html.RenderPartial("EspecialidadeTable", @ViewData);


            {
                ClinicaMVC3.Models.RegisterModel rm = new ClinicaMVC3.Models.RegisterModel();

                if (Model != null)
                {
                    rm.UserName = Model.aspnet_Users.UserName;
                    rm.Email = Model.aspnet_Users.aspnet_Membership.Email;
                    rm.Password = Model.aspnet_Users.aspnet_Membership.Password;

                }
                Html.RenderPartial("RegisterPartial", rm);

            }    
        
    <div id="footercrud">
        @if (ViewBag.Method == "Detail")
        {
        @Html.ActionLink("Editar", "Edit", new { id = Model.FuncionarioId });
        }
        else
        {
        <input type="button" value="Salvar" onclick="Save()" />
        }        
        @Html.ActionLink("Voltar", "Index")
    </div>
    
}
